# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql


class MysqlPipeline:
    def __init__(self):
        self.conntect = pymysql.Connect(
                host="localhost",
                port=3306,
                user="root",
                passwd="123456",
                db="misoft",
                charset="utf8"
        )
        self.cur = self.conntect.cursor()

    def process_item(self, item, spider):
        sql ="insert into softdetail(appid,logo,name,size,version,author,updatetime,packagename,images,info,download) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        tuple = (item['appid'],item['logo'],item['name'],item['size'],item['version'],item['author'],item['updatetime'],item['packagename'],item['images'],item['info'],item['download'])
        try:
            self.cur.execute(sql,tuple)
            self.conntect.commit()
        except:
            self.conntect.rollback()
        return item

    def close_spider(spider):
        self.cur.close()
        self.conntect.close()
